package com.banyou.banyou.order.evaluate;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v7.app.AlertDialog;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.banyou.banyou.R;
import com.banyou.banyou.base.BaseActivity;
import com.banyou.banyou.bean.order.EvaluateInfo;
import com.banyou.banyou.main.MainActivity;
import com.banyou.banyou.util.Constants;
import com.banyou.banyou.util.DialogFactory;
import com.banyou.banyou.util.MyActivityManager;
import com.banyou.banyou.util.RxBus;
import com.banyou.banyou.util.ShowToast;
import com.hedgehog.ratingbar.RatingBar;
import com.socks.library.KLog;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * ----------Dragon be here!----------/
 * 　　　┏┓　　　┏┓
 * 　　┏┛┻━━━┛┻┓
 * 　　┃　　　　　　　┃
 * 　　┃　　　━　　　┃
 * 　　┃　┳┛　┗┳　┃
 * 　　┃　　　　　　　┃
 * 　　┃　　　┻　　　┃
 * 　　┃　　　　　　　┃
 * 　　┗━┓　　　┏━┛
 * 　　　　┃　　　┃神兽保佑
 * 　　　　┃　　　┃代码无BUG！
 * 　　　　┃　　　┗━━━┓
 * 　　　　┃　　　　　　　┣┓
 * 　　　　┃　　　　　　　┏┛
 * 　　　　┗┓┓┏━┳┓┏┛
 * 　　　　　┃┫┫　┃┫┫
 * 　　　　　┗┻┛　┗┻┛
 * ━━━━━━神兽出没━━━━━━
 * 类描述：
 * 创建人：Random
 * 修改时间：
 * 修改备注：
 */

public class EvaluateActivity extends BaseActivity<EvaluatePresenter> implements EvaluateContract.View {

    @BindView(R.id.back_btn)
    ImageView mBackBtn;
    @BindView(R.id.evaluate_btn)
    Button mEvaluateBtn;
    @BindView(R.id.edit_main)
    EditText mEditMain;
    @BindView(R.id.main_button)
    Button mMainButton;
    @BindView(R.id.service_title)
    TextView mServiceTitle;
    @BindView(R.id.ratingbar)
    RatingBar mRatingBar;

    private int mStar;
    private String mUserType;
    private long mOrderId;

    @Override
    protected EvaluatePresenter initPresenter() {
        return new EvaluatePresenter();
    }

    @Override
    protected int initLayout() {
        return R.layout.activity_evaluate;
    }

    @Override
    protected View initLayoutView() {
        return null;
    }

    @Override
    protected void init() {
        mPresenter.initPresenter(this, new EvaluateModel());
        ButterKnife.bind(this);
        mUserType = getSharedPreference().getString("userType", "");
        mOrderId =getIntent().getLongExtra("orderId",0);
        KLog.a("orderId=" + mOrderId);
        mRatingBar.setOnRatingChangeListener(new RatingBar.OnRatingChangeListener() {
            @Override
            public void onRatingChange(float RatingCount) {
                mStar= (int) RatingCount;
                KLog.a("star="+mStar+",count="+RatingCount);
            }
        });
        if(getSharedPreference().getString("userType","").equals("1")){
            mServiceTitle.setText("对伴游的印象：");
        }
    }

    @Override
    protected String pageName() {
        return "评价页面";
    }


    @OnClick({R.id.back_btn, R.id.evaluate_btn, R.id.main_button})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.back_btn:
                Intent intent=new Intent(this, MainActivity.class);
                startActivity(intent);
                RxBus.getInstance().send(Constants.REFRESH_LIST_FRAGMENT);
                break;
            case R.id.evaluate_btn:
                createDialog();
                break;
            case R.id.main_button:
                if (mEditMain.getText().toString().trim().length() != 0) {
                    createEditDialog();
                } else {
                    ShowToast.showToast("请先填写评价");
                }
                break;
        }
    }

    private AlertDialog mDialog;

    /**
     * 确定发送dialog
     */
    private void createEditDialog() {
//        if (mStar == 0) {
//            ShowToast.showToast("请先选择星级");
//            return;
//        }
        DialogFactory factory = new DialogFactory(this);
        factory.setCancleAble(true);
        mDialog = factory.createDialog("确定评价么");
        factory.setSureBtnText("确定");
        factory.setCancleBtnText("取消");
        factory.setOnBtnClickListener(new DialogFactory.OnBtnClickListener() {
            @Override
            public void onSureClick(View view) {
                mDialog.dismiss();
//                if(mUserType.equals("1")) {
//                    mPresenter.score(getIntent().getIntExtra("orderId", 0) + "", mStar + "", mEditMain.getText().toString());
//                }else{
                if(mStar==0&&mEditMain.getText().toString().trim().length()==0){
                    ShowToast.showToast("必须填写一种评价");
                    return;
                }
                EvaluateInfo info = new EvaluateInfo();
                info.setOrderId(mOrderId);
                EvaluateInfo.SysUserCommentsBean bean1 = new EvaluateInfo.SysUserCommentsBean();
                List<EvaluateInfo.SysUserCommentsBean> list = new ArrayList<EvaluateInfo.SysUserCommentsBean>();
                if(mStar!=0) {
                    EvaluateInfo.SysUserCommentsBean bean2 = new EvaluateInfo.SysUserCommentsBean();
                    bean2.setCommentVal(mStar);
                    list.add(bean2);
                }
                bean1.setCommentDesc(mEditMain.getText().toString());
                list.add(bean1);
                info.setSysUserComments(list);
                Map<String, String> map = new HashMap<>();
                map.put("requestInfo", JSON.toJSONString(info));
                map.put("userType", mUserType);
                mPresenter.scoreToUser(map);
//                }

            }

            @Override
            public void onCancleClick(View view) {
                mDialog.dismiss();

            }
        });
        mDialog.show();
    }

    /**
     * 弹出星级选择框
     */
    private void createDialog() {

    }

    /**
     * 评分成功
     */
    @Override
    public void scoreSuccess() {
        ShowToast.showToast("评分成功！");
        RxBus.getInstance().send(Constants.REFRESH_LIST_FRAGMENT);
        MyActivityManager.getInstance().popNumActivity(2);
    }

    /**
     * 对游客评分成功
     */
    @Override
    public void scoreToUserSuccess() {
        ShowToast.showToast("评价成功！");
        RxBus.getInstance().send(Constants.REFRESH_LIST_FRAGMENT);
        MyActivityManager.getInstance().popNumActivity(2);
    }
}
